package com.ace.token.api.restful;

import com.ace.token.common.bean.RequestDTO;
import com.ace.token.common.bean.TokenDTO;
import com.ace.token.common.constant.C;
import com.ace.token.common.constant.Group;
import com.ace.token.module.entity.TokenDO;
import com.ace.token.module.service.TokenService;
import com.alibaba.fastjson.JSONObject;
import com.ace.token.common.bean.ObjectListDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * token控制层
 *
 * @Author
 * @Date 2018年07月02日 下午5:39
 */
@RestController
@RequestMapping("api/v1/token")
public class TokenController {

    @Autowired
    private TokenService tokenService;

    /**
     * 单个添加token
     *
     * @param requestDTO
     * @return
     */
    @PutMapping("add")
    public ObjectListDTO addToken(@RequestBody @Validated({Group.Add.class}) RequestDTO<TokenDTO> requestDTO) {
        TokenDTO tokenDTO = requestDTO.getData();
        TokenDO tokenDO = new TokenDO().setUserId(tokenDTO.getUserId()).setType(tokenDTO.getType());
        tokenService.add(tokenDO);
        ObjectListDTO resultsList = new ObjectListDTO();
        List<JSONObject> list = new ArrayList<>(C.N_2);
        JSONObject data = new JSONObject()
                .fluentPut("id", tokenDO.getUserId())
                .fluentPut("value", tokenDO.getValue())
                .fluentPut("type", tokenDO.getType());
        list.add(data);
        resultsList.setSize(C.N_1);
        resultsList.setList(list);
        return resultsList;
    }

    /**
     * 根据UserId和type删除token
     *
     * @param requestDTO
     * @return
     */
    @DeleteMapping("uid/type")
    public void deleteTokenByUserIdAndType(@RequestBody @Validated({Group.Default.class}) RequestDTO<TokenDTO> requestDTO) {
        TokenDTO tokenDTO = requestDTO.getData();
        TokenDO tokenDO = new TokenDO().setUserId(tokenDTO.getUserId()).setType(tokenDTO.getType());
        tokenService.deleteByUserIdAndType(tokenDO);
    }


    /**
     * 根据UserId删除这个用户下面的所有token
     *
     * @param requestDTO
     * @return
     */
    @DeleteMapping("uid")
    public void deleteTokenByUserId(@RequestBody @Validated({Group.Default.class}) RequestDTO<TokenDTO> requestDTO) {
        TokenDTO tokenDTO = requestDTO.getData();
        TokenDO tokenDO = new TokenDO().setUserId(tokenDTO.getUserId());
        tokenService.deleteByUserId(tokenDO);
    }


    /**
     * 单条删除token
     *
     * @param requestDTO
     * @return
     */
    @DeleteMapping("token")
    public void deleteToken(@RequestBody @Validated({Group.Default.class}) RequestDTO<TokenDTO> requestDTO) {
        TokenDTO tokenDTO = requestDTO.getData();
        TokenDO tokenDO = new TokenDO().setValue(tokenDTO.getValue());
        tokenService.deleteByTokenList(tokenDO);
    }

    /**
     * 校验token，校验通过可选择更新token时间
     *
     * @return
     **/
    @PostMapping("check")
    public void checkToken(@RequestBody @Validated({Group.Default.class, Group.Check.class}) RequestDTO<TokenDTO> requestDTO) {
        TokenDTO tokenDTO = requestDTO.getData();
        TokenDO tokenDO = new TokenDO().setValue(tokenDTO.getValue());
        boolean isUpdateTime = requestDTO.getData().getActive();
        tokenService.checkToken(tokenDO, isUpdateTime);
    }

    /**
     * 根据token查询用户id
     *
     * @return
     */
    @GetMapping("uid")
    public JSONObject getUserIdByTokenId(@RequestBody @Validated({Group.Default.class}) RequestDTO<TokenDTO> requestDTO) {
        TokenDTO tokenDTO = requestDTO.getData();
        TokenDO tokenDO = new TokenDO().setValue(tokenDTO.getValue());
        String userId = tokenService.selectByToken(tokenDO);
        JSONObject data = new JSONObject();
        data.put("userId", userId);
        return data;
    }
    /**
     * 查询所有token
     *
     * @return
     */
    @GetMapping("token")
    public JSONObject getTokenAll() {
        List<TokenDO> tokens = tokenService.selectAll();
        JSONObject data = new JSONObject();
        data.put("list", tokens);
        data.put("size", tokens.size());
        return data;
    }
}
